home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / text / hyper / ADtoHT2_0.lha / MyLib.lha / startup / _startup.c next >
Encoding:
C/C++ Source or Header  |  1995-03-14  |  2.7 KB  |  127 lines

  1. #ifndef DOS_DOSEXTENS_H
  2. #include <dos/dosextens.h>
  3. #endif
  4.  
  5. #ifndef WORKBENCH_STARTUP_H
  6. #include <workbench/startup.h>
  7. #endif
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11.  
  12. #if defined(__SASC_510)
  13. void NewList(struct List *);
  14. #elif defined(__GNUC__)
  15. #include <proto/alib.h>
  16. #else
  17. #include <clib/alib_protos.h>
  18. #endif
  19.  
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <Amiga.h>
  23.  
  24. #include "Internal.h"
  25.  
  26. /************************************************************************/
  27.  
  28. int AmigaMain(void);            /* main program */
  29. void __exit(int) __NORETURN;        /* "real" exit: restore stack and return */
  30. void _Startup(void) __NORETURN;        /* C part of startup code; implemented below */
  31.  
  32. /************************************************************************/
  33.  
  34. struct ExecBase *SysBase;
  35. struct WBStartup *WorkbenchMessage;
  36. struct DosLibrary *DOSBase;
  37. struct Library *UtilityBase;
  38.  
  39. struct MinList __stdioList;
  40.  
  41. FILE __StdFiles[3];
  42.  
  43. void (*MemoryCleanup)(void *);
  44.  
  45. /************************************************************************/
  46.  
  47. void _Startup(void)
  48.  
  49. {
  50.   struct Process *CurrentProcess;
  51.  
  52.   SysBase=*(struct ExecBase **)4;
  53.   CurrentProcess=(struct Process *)FindTask(NULL);
  54.  
  55.   if (!CurrentProcess->pr_CLI)
  56.     {
  57.       do
  58.     {
  59.       WaitPort(&CurrentProcess->pr_MsgPort);
  60.     }
  61.       while (!(WorkbenchMessage=(struct WBStartup *)GetMsg(&CurrentProcess->pr_MsgPort)));
  62.     }
  63.  
  64.   if ((DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",37)))
  65.     {
  66.       if ((UtilityBase=OpenLibrary("utility.library",37)))
  67.     {
  68.       NewList((struct List *)&__stdioList);
  69.       __StdFiles[0].Filehandle=Input();
  70.       __StdFiles[1].Filehandle=Output();
  71.       if (!(__StdFiles[2].Filehandle=CurrentProcess->pr_CES))
  72.         {
  73.           if (!(__StdFiles[2].Filehandle=Open("CONSOLE:",MODE_NEWFILE)))
  74.         {
  75.           __StdFiles[2].Filehandle=Output();
  76.         }
  77.           else
  78.         {
  79.           __StdFiles[2].Flags.Close=1;
  80.         }
  81.         }
  82.       AddTail((struct List *)&__stdioList,(struct Node *)&__StdFiles[0].Node);
  83.       AddTail((struct List *)&__stdioList,(struct Node *)&__StdFiles[1].Node);
  84.       AddTail((struct List *)&__stdioList,(struct Node *)&__StdFiles[2].Node);
  85.       exit(AmigaMain());
  86.     }
  87.     }
  88.   exit(100);
  89.   /* not reached */
  90. }
  91.  
  92. /************************************************************************/
  93.  
  94. void exit(int RC)
  95.  
  96. {
  97.   FILE *CurrentFile;
  98.  
  99.   for (CurrentFile=(FILE *)__stdioList.mlh_Head;
  100.        CurrentFile->Node.mln_Succ;
  101.        CurrentFile=(FILE *)CurrentFile->Node.mln_Succ)
  102.     {
  103.       if (CurrentFile->Flags.Close)
  104.     {
  105.       Close(CurrentFile->Filehandle);
  106.     }
  107.     }
  108.  
  109.   if (MemoryCleanup)
  110.     {
  111.       MemoryCleanup(__MemoryPool);
  112.     }
  113.  
  114.   if (DOSBase)
  115.     {
  116.       CloseLibrary(&DOSBase->dl_lib);
  117.       CloseLibrary(UtilityBase);
  118.     }
  119.  
  120.   if (WorkbenchMessage)
  121.     {
  122.       ReplyMsg(&WorkbenchMessage->sm_Message);
  123.     }
  124.  
  125.   __exit(RC);
  126. }
  127.